package com.baseweb.webadmin.controller.web;

import com.baseweb.webadmin.core.model.bvo.UserForm;
import com.baseweb.webadmin.core.model.pojo.*;
import com.baseweb.webadmin.core.util.TreeResc;
import com.baseweb.webadmin.service.UserService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dsy62 on 2017-07-15.
 * 用户信息
 */
@Controller
@RequestMapping(value = "/usersWeb")
public class SystemUserWebContoller {

    @Autowired
    private UserService userService;

    /**
     * 查询用户的钱包流水 根据userId Page
     * */
    @RequestMapping(value = "/findAllMoneyLogByUserIdPage", method = RequestMethod.POST)
    public String findAllMoneyLogByUserIdPage(Model model,@RequestParam("userid") int userid,@RequestParam("page") int page,@RequestParam("pageNum") int pageNum){
        List<ByMoneyLog> byMoneyLogList = userService.findAllMoneyLogByUserIdPage(userid,(page-1)*pageNum,pageNum);
        //总记录数
        int numSum = userService.countMoneyLogByUserIdPage(userid);
        //总页数
        int pageSum = (numSum>pageNum?(numSum/pageNum):1);
        if(numSum % pageNum > 0 && numSum>pageNum){
            pageSum += 1;
        }
        model.addAttribute("byMoneyLogList",byMoneyLogList);
        model.addAttribute("numSum",numSum);
        model.addAttribute("pageSum",pageSum);
        return "";
    }

    /**
     * 删除银行卡信息
     * */
    @RequestMapping(value = "/deleteCardById", method = RequestMethod.GET)
    public String deleteCardById(Model model,@RequestParam("id") int id){
        String isOK = "NO";
        if(userService.deleteCardById(id) > 0){
            isOK = "OK";
            model.addAttribute("msg","操作成功");
        }else {
            model.addAttribute("msg","操作失败");
        }
        model.addAttribute("isOK",isOK);
        return "";
    }

    /**
     * 银行卡查询全部根据userid 带分页
     * */
    @RequestMapping(value = "/findAllCardByUserIdPage", method = RequestMethod.POST)
    public String findAllCardByUserIdPage(Model model,@RequestParam("userid") int userid,@RequestParam("page") int page,@RequestParam("pageNum") int pageNum){
        List<Card> cardList = userService.findAllCardByUserIdPage(userid,(page-1)*pageNum,pageNum);
        //总记录数
        int numSum = userService.countCardByUserIdPage(userid);
        //总页数
        int pageSum = (numSum>pageNum?(numSum/pageNum):1);
        if(numSum % pageNum > 0 && numSum>pageNum){
            pageSum += 1;
        }
        model.addAttribute("cardList",cardList);
        model.addAttribute("numSum",numSum);
        model.addAttribute("pageSum",pageSum);
        return "";
    }

    /**
     * 查询提现记录根据userid page
     * */
    @RequestMapping(value = "/findAllCardLogByUserIdPage", method = RequestMethod.POST)
    public String findAllCardLogByUserIdPage(Model model,@RequestParam("userid") int userid,@RequestParam("status") String status,@RequestParam("page") int page,@RequestParam("pageNum") int pageNum){
        List<CardLog> cardLogList = userService.findAllCardLogByUserIdPage(status,userid,(page-1)*pageNum,pageNum);
        //总记录数
        int numSum = userService.countCardLogByUserIdPage(status,userid);
        //总页数
        int pageSum = (numSum>pageNum?(numSum/pageNum):1);
        if(numSum % pageNum > 0 && numSum>pageNum){
            pageSum += 1;
        }
        model.addAttribute("cardLogList",cardLogList);
        model.addAttribute("numSum",numSum);
        model.addAttribute("pageSum",pageSum);
        return "";
    }

    /**
     * 根据关键字查询关键字用户
     */
    @RequestMapping(value = "/findAllUserChannelByUserId", method = RequestMethod.POST)
    public String findAllUserChannelByUserId(Model model,@RequestParam("channelId") int channelId,@RequestParam("page") int page,@RequestParam("pageNum") int pageNum){
        List<User> userList = userService.findAllUserChannelByUserId(channelId,(page-1)*pageNum,pageNum);
        //总记录数
        int numSum = userService.countUserChannelByUserId(channelId);
        //总页数
        int pageSum = (numSum>pageNum?(numSum/pageNum):1);
        if(numSum % pageNum > 0 && numSum>pageNum){
            pageSum += 1;
        }
        model.addAttribute("userList",userList);
        model.addAttribute("numSum",numSum);
        model.addAttribute("pageSum",pageSum);
        return "";
    }

    /**
     * 根据邀请和状态查询所有用户信息
     */
    @RequestMapping(value = "/findAllUserListByMobileStatusPage", method = RequestMethod.POST)
    public String findAllUserListByMobileStatusPage(Model model, @RequestParam("status") int status,@RequestParam("mobile") String mobile,@RequestParam("page") int page,@RequestParam("pageNum") int pageNum){
        List<User> userList = userService.findAllUserListByMobileStatusPage(status,mobile,(page-1)*pageNum,pageNum);
        //总记录数
        int numSum = userService.countUserListByMobileStatusPage(status,mobile);
        //总页数
        int pageSum = (numSum>pageNum?(numSum/pageNum):1);
        if(numSum % pageNum > 0 && numSum>pageNum){
            pageSum += 1;
        }
        model.addAttribute("userList",userList);
        model.addAttribute("numSum",numSum);
        model.addAttribute("pageSum",pageSum);
        return "";
    }

    /**
     * 根据区域和用户类型查询所有用户信息
     */
    @RequestMapping(value = "/findAllUserByAreaType", method = RequestMethod.POST)
    public String findAllUserByAreaType(Model model,@RequestParam("area") String area){
        List<User> agents = userService.findAllUserByArea(area);
        model.addAttribute("agents",agents);
        return "";
    }

    /**
     * 删除推荐团队
     */
    @RequestMapping(value = "/deleteByPrimaryKeyRecommed", method = RequestMethod.POST)
    public String deleteByPrimaryKeyRecommed(Model model,@RequestParam("recommedId") int recommedId){
        String isOK = "NO";
        if(userService.deleteByPrimaryKeyRecommed(recommedId) > 0){
            isOK = "OK";
        }else {
            model.addAttribute("msg","操作失败");
        }
        model.addAttribute("isOK",isOK);
        return "";
    }

    /**
     * 添加/修改推荐团队
     */
    @RequestMapping(value = "/insertSelectiveRecommend", method = RequestMethod.POST)
    public String insertSelectiveRecommend(HttpServletRequest request,Model model,@ModelAttribute()ByUserRecommend byUserRecommend){
        String isOK = "NO";
        HttpSession session = request.getSession();
        if(session.getAttribute("login_id") != null){
            String userId = String.valueOf(session.getAttribute("login_id"));
            byUserRecommend.setUserId(Integer.valueOf(userId));
            //新增
            if(byUserRecommend.getId() == 0){
                User user = userService.findUserByUserName(byUserRecommend.getUserName());
                if(user != null){
                    if(userService.findByUserRecommendByUserIdRecommendId(Integer.valueOf(userId),user.getId())!=null){
                        model.addAttribute("msg","推荐团队重复");
                    }else {
                        byUserRecommend.setRecommendId(user.getId());
                        byUserRecommend.setCreateTime(new DateTime());
                        byUserRecommend.setUpdateTime(new DateTime());
                        byUserRecommend.setStatus(1);
                        if(userService.insertSelectiveRecommend(byUserRecommend) > 0){
                            isOK = "OK";
                        }else {
                            model.addAttribute("msg","操作失败");
                        }
                    }
                }
            }else {
                //修改
                byUserRecommend.setUpdateTime(new DateTime());
                if(userService.updateByPrimaryKeySelectiveRecommend(byUserRecommend) > 0){
                    isOK = "OK";
                }else {
                    model.addAttribute("msg","操作失败");
                }
            }
        }

        model.addAttribute("isOK",isOK);
        return "";
    }

    /**
     * 根据查询userId账号的所有推荐团队(分页)
     */
    @RequestMapping(value = "/findAllRecommendByUserIdPage", method = RequestMethod.POST)
    public String findAllRecommendByUserIdPage(HttpServletRequest request,Model model,@RequestParam("page") int page,@RequestParam("pageNum") int pageNum){
        String isOK = "NO";
        HttpSession session = request.getSession();
        if(session.getAttribute("login_id") != null){
            String userId = String.valueOf(session.getAttribute("login_id"));
            List<ByUserRecommend> list = userService.findAllRecommendByUserIdPage(Integer.valueOf(userId),(page-1)*pageNum,pageNum);
            //总记录数
            int numSum = userService.countRecommendByUserIdPage(Integer.valueOf(userId));
            //总页数
            int pageSum = (numSum>pageNum?(numSum/pageNum):1);
            if(numSum % pageNum > 0 && numSum>pageNum){
                pageSum += 1;
            }
            isOK = "OK";
            model.addAttribute("recommendList",list);
            model.addAttribute("numSum",numSum);
            model.addAttribute("pageSum",pageSum);
        }
        model.addAttribute("isOK",isOK);
        return "";
    }

    /**
     * 根据当前登录账号拥有权限 查询即将授权的角色的所有功能权限 带已经授权状态
     */
    @RequestMapping(value = "/findAllRescByRoleIdFromUserId", method = RequestMethod.POST)
    public String findAllRescByRoleIdFromUserId(HttpServletRequest request,Model model,@RequestParam("roleId") int roleId){
        String isOK = "NO";
        HttpSession session = request.getSession();
        if(session.getAttribute("login_id") != null){
            String userId = String.valueOf(session.getAttribute("login_id"));
            List<Resc> list = userService.findAllRescByRoleIdFromUserId(Integer.valueOf(userId),roleId);
            List<Resc> rescList = new TreeResc().buildTree(list);
            isOK = "OK";
            model.addAttribute("rescList", list);
            model.addAttribute("rescMenuList", rescList);
        }
        model.addAttribute("isOK", isOK);
        return "";
    }

    /**
     * 角色系统功能授权
     */
    @RequestMapping(value = "/insertIntoRescRole", method = RequestMethod.POST)
    public String insertIntoRescRole(Model model,@RequestParam("roleId") int roleId,@RequestParam("rescs") String rescs){
        String isOK = "NO";
        if(!"".equals(rescs)){
            String[] resc=rescs.split(",");
            //删除已有
            userService.deleteRescRoleByRoleId(roleId);
            //添加
            for(int i=0;i<resc.length;i++){
                userService.insertIntoRescRole(Integer.valueOf(resc[i]),roleId);
            }
            isOK = "OK";
        }
        model.addAttribute("isOK", isOK);
        return "";
    }

    /**
     * 根据根据user关联查询关键字
     */
    @RequestMapping(value = "/findAllByChannelKeywordByUserChannel", method = RequestMethod.POST)
    public String findAllByChannelKeywordByUserChannel(Model model,@RequestParam("userName") String userName){
        if(!"".equals(userName)){
            List<ByChannelKeyword> byChannelKeywordList = userService.findAllByChannelKeywordByUserChannel(userName);
            model.addAttribute("byChannelKeywordList",byChannelKeywordList);
        }
        return "";
    }

    /**
     * 根据搜索id查询所有用户信息
     */
    @RequestMapping(value = "/findAllUserFormByChannelId", method = RequestMethod.POST)
    public String findAllUserFormByChannelId(Model model,@RequestParam("channelId") int channelId){
        List<User> userList = userService.findAllUserFormByChannelId(channelId);
        model.addAttribute("userList",userList);
        return "";
    }

    /**
     *根据username查询用户信息
     */
    @RequestMapping(value = "/findUserByUserName", method = RequestMethod.POST)
    public String findUserByUserName(Model model,@RequestParam("userName") String userName){
        User user = userService.findUserByUserName(userName);
        model.addAttribute("user",user);
        return "";
    }

    /**
     * 修改用户信息
     */
    @RequestMapping(value = "/updateUserWeb", method = RequestMethod.POST)
    public String updateUserWeb(Model model,@ModelAttribute()User user){
        String isOK = "NO";
        if(user != null){
            User userV = userService.selectByUserName(user.getUsername());
            user.setId(userV.getId());
            if(userService.updateUser(user) == 1){
                isOK = "OK";
            }else {
                model.addAttribute("msg","操作失败");
            }
        }else {
            model.addAttribute("msg","参数有误");
        }
        model.addAttribute("isOK",isOK);
        return "";
    }


    /**
     * 根据状态查询客户详细信息所有
     */
    @RequestMapping(value = "/findAllUserFormByStatusPage", method = RequestMethod.GET)
    public String findAllUserFormByStatusPage(Model model,@RequestParam("username") String username,@RequestParam("status") int status, @RequestParam("page") int page, @RequestParam("pageNum") int pageNum){
        List<User> userList = userService.findAllUserFormByStatusPage(status,username,(page-1)*pageNum,pageNum);
        //总记录数
        int numSum = userService.countUserFormByStatusPage(status,username);
        //总页数
        int pageSum = (numSum>pageNum?(numSum/pageNum):1);
        if(numSum % pageNum > 0 && numSum>pageNum){
            pageSum += 1;
        }
        model.addAttribute("userList",userList);
        model.addAttribute("numSum",numSum);
        model.addAttribute("pageSum",pageSum);
        return "";
    }

    /**
     * 查询用户信息
     */
    @RequestMapping(value = "/findAllUsers", method = RequestMethod.GET)
    public String findAllUsers(HttpServletRequest request, Model model, @RequestParam("page") int page, @RequestParam("pageNum") int pageNum,
                               @RequestParam("cnName") String cnName,@RequestParam("trueName") String trueName,@RequestParam("idCard") String idCard,
                               @RequestParam("userNameWhere") String userNameWhere,@RequestParam("province") String province,@RequestParam("city") String city,
                               @RequestParam("area") String area){
        HttpSession session = request.getSession();
        if(session.getAttribute("login_name") != null){
            String userName = String.valueOf(session.getAttribute("login_name"));
            User user = userService.selectByUserName(userName);
            List<User> list = new ArrayList<User>();
            List<User> listR = new ArrayList<User>();
            //总记录数
            int numSum = 0;
            if(1 == user.getUserType()){
                list = userService.findAllUserFormByPage((page-1)*pageNum,pageNum,cnName,trueName,idCard,userNameWhere,province,city, area);
                numSum = userService.countUserFormByPage(cnName,trueName,idCard,userNameWhere,province,city, area);
            }else if(2 == user.getUserType()){
                list = userService.findUserFormByPageUserId(user.getId(),(page-1)*pageNum,pageNum,cnName,trueName,idCard,userNameWhere,province,city, area);
                numSum = userService.countUserFormByPageUserId(user.getId(),cnName,trueName,idCard,userNameWhere,province,city, area);
            }else if(3 == user.getUserType()){
                list = userService.findUserFormByPageUserId(user.getId(),(page-1)*pageNum,pageNum,cnName,trueName,idCard,userNameWhere,province,city, area);
                numSum = userService.countUserFormByPageUserId(user.getId(),cnName,trueName,idCard,userNameWhere,province,city, area);
            }else if(4 == user.getUserType()){
                list = userService.findUserFormByPageUserId(user.getId(),(page-1)*pageNum,pageNum,cnName,trueName,idCard,userNameWhere,province,city, area);
                numSum = userService.countUserFormByPageUserId(user.getId(),cnName,trueName,idCard,userNameWhere,province,city, area);
            }else if(5 == user.getUserType()){
                list = userService.findUserFormByPageArea(user.getId(),(page-1)*pageNum,pageNum,cnName,trueName,idCard,userNameWhere,province,city, area);
                numSum = userService.countUserFormByPageArea(user.getArea(),cnName,trueName,idCard,userNameWhere,province,city, area);
            }

            for(User user1 : list){
                List<ByUserBizAuth> byUserBizAuthList = userService.findUserBizAuthByUserId(user1.getId());
                String baName = "";
                for(ByUserBizAuth byUserBizAuth : byUserBizAuthList){
                    if(byUserBizAuth.getAuthType() == 1){
                        baName += "平台认证,";
                    }else if(byUserBizAuth.getAuthType() == 2){
                        baName += "诚信认证,";
                    }else if(byUserBizAuth.getAuthType() == 3){
                        baName += "学院认证,";
                    }
                }
                user1.setAuthName(baName);
                listR.add(user1);
            }
            //总页数
            int pageSum = (numSum>pageNum?(numSum/pageNum):1);
            if(numSum % pageNum > 0 && numSum>pageNum){
                pageSum += 1;
            }
            model.addAttribute("userList",listR);
            model.addAttribute("numSum",numSum);
            model.addAttribute("pageSum",pageSum);
        }
        return "";
    }
}
